import hashlib
import os
import zlib


def calculate_file_sha256(filename):
    with open(filename, 'rb') as file:
        data = file.read()
        sha256_hash = hashlib.sha256(data).hexdigest()

    return sha256_hash
def calculate_file_crc32(filename):
    with open(filename, 'rb') as file:
        data = file.read()
        crc32_value = zlib.crc32(data) & 0xffffffff

    return crc32_value
def calculate_file_md5(filename):
    md5 = hashlib.md5()

    with open(filename, 'rb') as file:
        while True:
            data = file.read(4096)  # 每次读取4KB数据

            if not data:
                break

            md5.update(data)

    return md5.hexdigest()
file_types = {

    ".aok": "application/vnd.android.package-archive",
    ".exe":"application/x-msdownload",
    ".ogg":"audio/ogg",
    ".ini": "text/plain",
    ".txt": "text/plain",
    ".lrc": "text/plain",
    ".csv": "text/csv",
    ".json": "application/json",
    ".xml": "application/xml",
    ".html": "text/html",
    ".md": "text/markdown",
    ".jpg": "image/jpeg",
    ".png": "image/png",
    ".gif": "image/gif",
    ".svg": "image/svg+xml",
    ".mp3": "audio/mpeg",
    ".wav": "audio/x-wav",
    ".mp4": "video/mp4",
    ".mkv": "video/mkv",
    ".webm": "video/webm",
    ".zip": "application/zip",
    ".tar.gz": "application/x-gzip",
    ".pdf": "application/pdf",
    ".js": "application/javascript",
    ".css": "text/css",
    ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    ".xls": "application/vnd.ms-excel",
    ".doc": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    ".ppt": "application/vnd.openxmlformats-officedocument.presentationml.presentation"
}

def get_extension(file_path):
    file_extension = file_path.split('.')[-1]
    file_extension = "." + file_extension
    return file_extension
def get_mimetype(file_path):
    file_extension = file_path.split('.')[-1]
    file_extension="."+file_extension
    if file_extension in file_types:
        return file_types[file_extension]
    else:
        return "application/octet-stream"

def 生成file列表(filepath,path=None):
    html = []
    html.append('\n'  
                '<!DOCTYPE html>  \n'  
                '<html>  \n'  
                '<head>  \n'  
                '  <title>'+"文件服务器"+'</title>  \n'  
                '</head>  \n'  
                '<body>  \n')
    if path!=None:
        html.append(f'<li><a href="/file?path={os.path.dirname(path)}">返回上一级/</a></li>')
    for path in filepath:
        if path[0]=="dir":
            html.append(f'<li><a href="/file?path={path[1]}">{path[1]}/</a></li>')
        elif path[0]=="file":
            html.append(f'<li><a href="/file?path={path[1]}">{path[1]}</a></li>')


    html.append('</body>  \n'  
                '</html>\n')
    return '\n'.join(html)
def 生成打开方式(filepath):
    html = []
    html.append('\n'  
                '<!DOCTYPE html>  \n'  
                '<html>  \n'  
                '<head>  \n'  
                '  <title>'+"打开方式"+'</title>  \n'  
                '</head>  \n'  
                '<body>  \n'
                )

    html.append(f'<p>你需要打开的文件是</p>')
    html.append(f'<p>文件后缀名:{get_extension(filepath)}</p>')
    html.append(f'<p>Content-Type:{get_mimetype(filepath)}</p>')
    html.append(f'<p><a href="/file?path={filepath}&action=download">下载文件</a></p>')
    html.append(f'<p><a href="/file?path={filepath}&action=view">在线查看</a></p>')
    html.append(f'<p><a href="/file?path={filepath}&action=md5">计算md5</a></p>')
    if get_extension(filepath) in [".mp4", ".mkv",".mp3",".rm",".mov",".ogg",".flac"]:
        html.append(f'<p><a href="/file?path={filepath}&action=play">远程播放</a></p>')
    html.append('</body>  \n'  
                '</html>\n')
    return '\n'.join(html)
def 生成网页对话框(title,text,网页内容标题,网页内容,跳转的网页="document.referrer"):
    警告 = ('\n'
           '<!DOCTYPE html>  \n'
           '<html>  \n'
           '<head>  \n'
           '  <title>'+title+'</title>  \n'
           '  <script>  \n'
           '    window.onload = function() {  \n'
           '      alert(\''+text+'\');  \n'
           '    };  \n'
           'setTimeout(() => {  \n'
           '  window.location.href= '+跳转的网页+';  \n'
           '}, 3000);'                                 
           '  </script>  \n'
           '</head>  \n'
           '<body>  \n'
           '  <h1>'+网页内容标题+'</h1>  \n'
           '  <p>'+网页内容+'</p>  \n'
           '</body>  \n'
           '</html>\n')
    return 警告